<template>
	<view class="container">
		<uni-forms ref="stockForm" :modelValue="info" class="form" label-width="120">
			<uni-forms-item label="任务编码" class="form-item">
				<view class="uni-input">{{info.num}}</view>
			</uni-forms-item>
			<uni-forms-item label="盘点要求" class="form-item">
				<view class="uni-input">{{info.remark}}</view>
			</uni-forms-item>
			<uni-forms-item label="开始时间" class="form-item">
				<view class="uni-input">{{info.startTime}}</view>
			</uni-forms-item>
			<uni-forms-item label="结束时间" class="form-item">
				<view class="uni-input">{{info.endTime}}</view>
			</uni-forms-item>
			<uni-forms-item label="负责人" class="form-item">
				<view class="uni-input">{{info.userName}}</view>
			</uni-forms-item>
			<uni-forms-item label="任务状态" class="form-item">
				<view class="uni-input">{{dictTag('asset_task_status', info.status)}}</view>
			</uni-forms-item>
			<template v-if="info.status=='2'">
			<uni-forms-item label="盘点结果" class="form-item" required>
				<uni-easyinput v-model="info.result" placeholder="请输入盘点结果" />
			</uni-forms-item>				
			</template>
			<template v-else>
			<uni-forms-item label="盘点结果" class="form-item">
				<view class="uni-input">{{info.result}}</view>
			</uni-forms-item>				
			<uni-forms-item label="固定资产" class="form-item">
				<view class="uni-input">{{info.assetCount}} 件，值{{info.assetWorth}}</view>
			</uni-forms-item>
			<uni-forms-item label="出售资产" class="form-item">
				<view class="uni-input">{{info.saleCount}} 件，值{{info.saleWorth}}</view>
			</uni-forms-item>
			<uni-forms-item label="报废资产" class="form-item">
				<view class="uni-input">{{info.scrapCount}} 件，值{{info.scrapWorth}}</view>
			</uni-forms-item>
			<uni-forms-item label="丢失资产" class="form-item">
				<view class="uni-input">{{info.loseCount}} 件，值{{info.loseWorth}}</view>
			</uni-forms-item>
			<uni-forms-item label="维修资产" class="form-item">
				<view class="uni-input">{{info.maintainCount}} 件，值{{info.maintainWorth}}</view>
			</uni-forms-item>
			</template>
		</uni-forms>
		<button type="primary" v-if="info.status == '0'" @click="handleStock()">开始盘点</button>	
		<template v-else-if="info.status == '2'">
			<uni-row>
				<uni-col :span="12">
					<view class="demo-uni-col dark">						
						<button type="primary" @click="handleStock()">继续盘点</button>
					</view>
				</uni-col>
				<uni-col :span="12">
					<view class="demo-uni-col light">
						<button type="warn" @click="handleSubmit()">盘点完成</button>						
					</view>
				</uni-col>
			</uni-row>
		</template>		
		<button type="primary" v-else @click="gotoAction(info.id, 'view')">查看明细</button>
	</view>
</template>


<script>
	import { getStock, updateStock} from '@/api/stock.js'
	import { queryDs } from '@/api/dataset.js'
	import { dictMixin } from '@/plugins/dict.js';
	export default {
    mixins: [dictMixin],
		data() {
			return {
				info: { },
				action: 'view',
				actionItems: [],
				yesNoOptions: [],
				statusOptions: [],
				statusIndex: 0				
			}
		},
		onLoad() {
			this.useDict(['asset_task_status']);
			
			getStock(this.$route.query.id).then(response => {
				this.info = response.data;				
			})
					
		},
		methods: {
			handleStock() {
				if(this.info.status == '0'){
					let that = this;
					this.$modal.confirm('当前盘点任务还没有开始，您是否要开始进行盘点？').then(function(){
						that.info.status = '2';
						updateStock(that.info).then(response=>{
							that.gotoAction(that.info.id, 'stock')
						});
					});
				}else if(this.info.status == '2'){
					this.gotoAction(this.info.id, 'stock')
				}else{
					this.$model.msgError('当前任务不能再进行盘点');
				}
			},
			handleSubmit(ref) {
				if(this.info.result == null || this.info.result == ''){
					this.$modal.msgError('请输入盘点结果');
					return;
				}
				let that = this;
				let data = {...this.info};
				data.status = '1';
				this.$modal.confirm('请确认全部资产已经盘点完成，提交后将不能再进行修改，您是否要提交数据？').then(function(){
					updateStock(data).then(response=>{
						that.$model.msgError('操作成功');
						that.info.status = '1';
					})
				});				
			},
			gotoAction(id, action) {
				let url = '/pages/stock/list?stockId='+id+'&action='+action;
				this.$tab.navigateTo(url);
			}
		}
	}
</script>

<style>
	.form {
		padding: 15px;
		background-color: #fff;
	}
	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		place-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>